package com.treasure.service;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

@SpringBootApplication
@EnableAutoConfiguration
@EnableScheduling
@EnableTransactionManagement
@EnableConfigurationProperties
@Configuration
@ServletComponentScan
@MapperScan("com.treasure.service.mapper")
//@EnableRedisHttpSession
@ComponentScan(basePackages = "com.treasure.service")
public class BusinessApplication extends SpringBootServletInitializer implements CommandLineRunner , Serializable {


    private static final long serialVersionUID = -3870212065304966318L;
    private Logger logger = LoggerFactory.getLogger(BusinessApplication.class);

    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(BusinessApplication.class, args);
//        ApplicationContext ctx = SpringApplication.run(WebApplication.class, args);
//        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
//        template.convertAndSend("jr.web.config.update", "staticVersion");
    }

    //设置session失效时间15分钟
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setSessionTimeout(900);//单位为S
            }
        };
    }

    @Override
    public void run(String... strings) throws Exception {
        try {
            logger.info("\n----------------------------------------------------------\n\t" +
                    "Application '{}' is running! Access URLs:\n\t" +
                    "Local: \t\thttp://127.0.0.1:{}\n\t" +
                    "External: \thttp://{}:{}\n\t" +
                    "Profile(s): \t{}\n----------------------------------------------------------",

                environment.getProperty("spring.application.name"),
                environment.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                environment.getProperty("server.port"),
                Arrays.toString(environment.getActiveProfiles()));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
    /**
     * xss过滤器
     * @return
     */
//    @Bean
//    public XssFilter preXssFilter() {
//        return new XssFilter();
//    }

}
